import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/common/res/constant.dart';
import 'package:oramemo/common/res/gaps.dart';
import 'package:oramemo/common/routes/routes.dart';
import 'package:oramemo/common/styles/text_styles.dart';
import 'package:oramemo/common/styles/text_styles.dart';
import 'package:oramemo/common/utils/change_notifier_manage.dart';
import 'package:oramemo/common/utils/keyboard_util.dart';
import 'package:oramemo/common/utils/shared_util.dart';
import 'package:oramemo/core/services/storage_service.dart';
import 'package:oramemo/pages/login/login/login_logic.dart';
import 'package:oramemo/pages/login/login/login_state.dart';
import 'package:oramemo/pages/login/widgets/my_text_field.dart';
import 'package:oramemo/widget/my_app_bar.dart';
import 'package:oramemo/widget/my_button.dart';
import 'package:oramemo/widget/my_scroll_view.dart';

import '../login_router.dart';

/// design/1注册登录/index.html
class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage>
    with ChangeNotifierMixin<LoginPage> {
  //定义一个controller
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  bool _clickable = false;
  final LoginLogic logic = Get.put(LoginLogic());
  final LoginState state = Get.find<LoginLogic>().state;

  Map<ChangeNotifier, List<VoidCallback>?>? changeNotifier() {
    final List<VoidCallback> callbacks = <VoidCallback>[_verify];
    return <ChangeNotifier, List<VoidCallback>?>{
      _nameController: callbacks,
      _passwordController: callbacks,
      _nodeText1: null,
      _nodeText2: null,
    };
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      /// 显示状态栏和导航栏
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
          overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
    });
    _nameController.text = StorageService.to.getUserName() ?? '';
  }

  void _verify() {
    final String name = _nameController.text;
    final String password = _passwordController.text;
    bool clickable = true;
    if (name.isEmpty || name.length < 4) {
      clickable = false;
    }
    if (password.isEmpty || password.length < 6) {
      clickable = false;
    }

    /// 状态不一样再刷新，避免不必要的setState
    if (clickable != _clickable) {
      setState(() {
        _clickable = clickable;
      });
    }
  }

  void _login() {
    logic.requestData(
        username: _nameController.value.text,
        password: _passwordController.value.text);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        actionName: TextKey.loginSMS.tr,
        onPressed: () {
          Get.toNamed(LoginRouter.smsLoginPage);
        },
      ),
      body: MyScrollView(
        keyboardConfig: KeyboardUtil.getKeyboardActionsConfig(
            context, <FocusNode>[_nodeText1, _nodeText2]),
        padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 20.0),
        children: _buildBody,
      ),
    );
  }

  List<Widget> get _buildBody => <Widget>[
        Text(
          TextKey.loginTitle.tr,
          style: TextStyles.textBold26,
        ),
        Gaps.vGap16,
        MyTextField(
          key: const Key('phone'),
          focusNode: _nodeText1,
          controller: _nameController,
          maxLength: 64,
          keyboardType: TextInputType.visiblePassword,
          hintText: TextKey.inputPhoneOrEmail.tr,
        ),
        Gaps.vGap8,
        MyTextField(
          key: const Key('password'),
          keyName: 'password',
          focusNode: _nodeText2,
          isInputPwd: true,
          controller: _passwordController,
          keyboardType: TextInputType.visiblePassword,
          hintText: TextKey.inputPassword.tr,
        ),
        Gaps.vGap24,
        MyButton(
          key: const Key('login'),
          radius: 8,
          onPressed: _clickable ? _login : null,
          text: TextKey.loginButton.tr,
        ),
        // Container(
        //   height: 40.0,
        //   alignment: Alignment.centerRight,
        //   child: GestureDetector(
        //     child: Text(
        //       TextKey.loginForgetPassword.tr,
        //       key: const Key('forgotPassword'),
        //       style: Theme.of(context).textTheme.titleSmall,
        //     ),
        //     onTap: () => Get.toNamed(LoginRouter.resetPasswordPage),
        //   ),
        // ),
        Gaps.vGap16,
        // Container(
        //     alignment: Alignment.center,
        //     child: GestureDetector(
        //       child: Text(
        //         TextKey.loginNoAccount.tr,
        //         key: const Key('noAccountRegister'),
        //         style: TextStyle(color: Theme.of(context).primaryColor),
        //       ),
        //       onTap: () => Get.toNamed(LoginRouter.smsLoginPage),
        //     ))
      ];
}
